package com.kayo.specimen.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kayo.specimen.annotion.JsonParam;
import com.kayo.specimen.annotion.PagingParam;
import com.kayo.specimen.model.JsonResult;
import com.kayo.specimen.model.Privilege;
import com.kayo.specimen.service.PrivilegeService;
import com.kayo.specimen.utility.PagingInfo;
import com.kayo.specimen.utility.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.constraints.NotEmpty;
import java.util.List;

@RequiresPermissions("读取权限")
@RestController
@RequestMapping("/privilege")
public class PrivilegeController {

    @Autowired
    private PrivilegeService privilegeService;

    @RequestMapping("/index")
    public ModelAndView index() {
        return new ModelAndView("/views/privilege/list");
    }

    @RequestMapping("/list")
    public Object list(String id, String name, String url, String dependentId, @PagingParam PagingInfo pagingInfo) {
        List<Privilege> privileges = privilegeService.getPrivileges(id, name, url, dependentId, pagingInfo.getOrderByClause());
        PageHelper.startPage(pagingInfo.getPage(), pagingInfo.getLimit());
        PageInfo<Privilege> privilegePageInfo = new PageInfo<>(privileges);
        return JsonResult.success(privilegePageInfo);
    }

    @RequiresPermissions("更新权限")
    @RequestMapping("/add")
    public ModelAndView add() {
        Privilege privilege = new Privilege();
        ModelAndView modelAndView = new ModelAndView("/views/privilege/edit");
        modelAndView.addObject("privilege", privilege);
        return modelAndView;
    }

    @RequiresPermissions("更新权限")
    @RequestMapping("/edit")
    public ModelAndView edit(@NotEmpty(message = "id不能为空")String id) {
        List<Privilege> privileges = privilegeService.getPrivileges(id, null, null, null, null);
        ModelAndView modelAndView = new ModelAndView("/views/privilege/edit");
        modelAndView.addObject("privilege", privileges.get(0));
        return modelAndView;
    }

    @RequiresPermissions("更新权限")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(@JsonParam Privilege privilege){
        if(StringUtils.isNullOrEmpty(privilege.getId()))
            privilegeService.addPrivileges(privilege);
        else
            privilegeService.updatePrivileges(privilege);
        return JsonResult.success();
    }

    @RequiresPermissions("删除权限")
    @RequestMapping("/delete")
    public Object delete(@NotEmpty(message = "id不能为空") @JsonParam String[] id){
        privilegeService.removePrivileges(id);
        return JsonResult.success();
    }
}
